package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;

import com.itheima.service.OrderSettingService;
import com.itheima.utils.HealthStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 预约管理业务层
 * @author: wangjingliang
 * @create: 2019-11-16 19:33
 **/
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * 将上传的文件内容导入数据库
     *
     * @param
     * @return: null
     * @date: 2019/11/17
     */
    @Override
    public void add(List<String[]> readExcelList) {
        if (readExcelList == null) {
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        //先对类型进行更改,转换成封装类型
        List<OrderSetting> orderSettingList = new ArrayList<>();

        for (String[] strings : readExcelList) {
            String orderDate = strings[0];
            Integer number = 0;
            Integer reservations = 0;
            if (strings.length > 1) {
                number = Integer.parseInt(strings[1]);
            }
            //如果设置的人数为负数则抛出提示
            if (number < 0) {
                throw new RuntimeException("您设置的可预约人数不正确");
            }
            //对orderDates的数据格式进行转换
            Date date = null;
            try {
                date = new SimpleDateFormat("yyyy/MM/dd").parse(orderDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            OrderSetting orderSetting = new OrderSetting(date, number, reservations);
            orderSettingList.add(orderSetting);
        }


        //对封装好的数据 进行遍历操作
        for (OrderSetting orderSetting : orderSettingList) {
            //对时间进行判断
            if (System.currentTimeMillis() > orderSetting.getOrderDate().getTime()) {
                //如果当前时间比传入的时间要大,说明是过去时,不对其进行数据的操作
                continue;
            }
            //判断是否有数据存在
            int countOrderSetting = orderSettingMapper.findCountOrderSetting(orderSetting);
            if (countOrderSetting == 0) {
                //不存在数据,对数据进行插入
                int add = orderSettingMapper.add(orderSetting);
                if (add == 0) {
                    throw new RuntimeException(MessageConstant.EXCEPION);
                }
            } else {
                //存在数据,对数据进行修改
                int edit = orderSettingMapper.edit(orderSetting);
                if (edit == 0) {
                    throw new RuntimeException(MessageConstant.EXCEPION);
                }
            }

        }

    }

    /**
     * 根据年月查询信息
     *
     * @param:
     * @return: null
     * @date: 2019/11/17
     */

    @Override
    public List<Map<String, Object>> findByorderDate(String date) {
        HealthStringUtils.paramisNull(date, MessageConstant.EXCEPION);
        List<Map<String, Object>> byorderDate = null;
        byorderDate = orderSettingMapper.findByorderDate(date);
        for (Map<String, Object> map : byorderDate) {
            if (!map.containsKey("reservations")) {
                map.put("reservations", 0);
            }
            if (!map.containsKey("number")) {
                map.put("number", 0);
            }
        }
        if (byorderDate == null) {
            throw new RuntimeException(MessageConstant.EXCEPION);
        }

        return byorderDate;
    }

    /**
     * 根据日期对预约人数进行修改
     *
     * @param:
     * @return: null
     * @date: 2019/11/17
     */
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {

        //判断预约设置的时间是否过长
        if (orderSetting.getOrderDate().getTime() > (System.currentTimeMillis() + (1000 * 60 * 60 * 24 * 30))) {
            throw new RuntimeException("你的设置不能超过一个月");
        }
        OrderSetting allByDate = null;
        //先查看原先是否有设置预约人数
        try {
            allByDate = orderSettingMapper.findAllByDate(orderSetting.getOrderDate());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }
        if (allByDate == null || allByDate.getOrderDate() == null) {
            throw new RuntimeException("该日期端暂未开放预约,请联系后台人员");
        }
        //重置预约人数不能小于已存在的人数
        if (allByDate.getReservations() > orderSetting.getNumber()) {
            throw new RuntimeException("重置的预约人数不得小于当前设置");
        }
        try {

            //当查询结果为0时说明之前没有设置可能是个null
            if (allByDate.getNumber() == 0) {
                orderSettingMapper.insertNumberByDate(orderSetting);
            } else {
                //不为0说明有数值
                orderSettingMapper.editNumberByDate(orderSetting);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(MessageConstant.EXCEPION);
        }

    }


}
